package eraserhead.boundary;

import java.awt.Font;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import eraserhead.controller.AddEdgeController;
import eraserhead.controller.StartRoundsController;
import eraserhead.entity.Decision;


/** GUI for the DL Event
 * 
 * @author Kevin, Quanquan
 *
 */

public class DecisionGUI extends JFrame{
	private static final long serialVersionUID = -5995615095674810365L;
	private JLabel questionTextField;
	private JButton startButton;
	private JLabel instructions;
	JTextField eventIDLabel;
	
	ChoicePanel choicePanel;
	DecisionLinesCanvas linesCanvas;
	
	// feel free to change, this keeps track of the "model"
	Decision model;
	// private LinesCanvas canvas;
	static DecisionGUI form = null;
	
	private DecisionGUI(Decision model) {
		this.model = model;
		initComponents();
	}

	public static DecisionGUI getInstance() {
		return form;
	}

	public static DecisionGUI getInstance(Decision model){
		if (model == null)
			return getInstance();
		else {
			form = new DecisionGUI(model);
			return form;
		}
	}
	

	public static boolean isAvailable() {
		return form == null;
	}
	
	public Decision getModel() {
		return model;
	}

	public void setModel(Decision model) {
		this.model = model;
	}

	public void initComponents() {
		// canvas = new LinesCanvas(m);
		// canvas.addMouseListener(new DrawEdgeController(this, m, canvas));
		
		setTitle("DecisionLine Client");
		setBounds(0, 0, 800, 600);
		setResizable(false);
		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		
		questionTextField = new JLabel();
		questionTextField.setFont(new Font("Felix Titling", Font.PLAIN, 24));
		questionTextField.setText(model.getQuestion());
		//questionTextField.setEditable(false);
		//questionTextField.setColumns(10);
		
		eventIDLabel = new JTextField("Event ID: ");
		eventIDLabel.setEditable(false);
		eventIDLabel.setFont(new Font("Felix Titling", Font.PLAIN, 14));
		
		instructions = new JLabel();
		instructions.setFont(new Font("Felix Titling", Font.PLAIN, 14));
		if (model.getSignedInAs() != null)
			instructions.setText("Welcome " + model.getSignedInAs().getName());
		
		linesCanvas = new DecisionLinesCanvas(model);
		linesCanvas.addMouseListener(new AddEdgeController(model, linesCanvas));
		
		choicePanel = new ChoicePanel(model);
		
		JLabel numberOfRoundsLabel = new JLabel("Number of Rounds: " + model.getNumRounds());
		numberOfRoundsLabel.setFont(new Font("Felix Titling", Font.PLAIN, 14));
		
		//JLabel lblErrorMessages = new JLabel("Error Message");
		//lblErrorMessages.setFont(new Font("Felix Titling", Font.PLAIN, 14));
		
		
		startButton = new JButton();
		startButton.setText("Start");
		startButton.setFont(new Font("Felix Titling", Font.PLAIN, 14));
		boolean modStatus = (model.getSignedInAs() == null) ? false : model.getSignedInAs().getModeratorStatus();
		startButton.setEnabled(modStatus);
		startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	new StartRoundsController(model, DecisionGUI.this).process();
            }
        });

		//errorMessage.setEditable(false);
		//errorMessage.setColumns(10);
		
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(55)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(choicePanel, GroupLayout.PREFERRED_SIZE, 448, GroupLayout.PREFERRED_SIZE)
							.addContainerGap())
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addComponent(questionTextField, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 448, Short.MAX_VALUE)
								.addComponent(linesCanvas, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 448, Short.MAX_VALUE))
							.addGap(45)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(startButton)
								.addComponent(numberOfRoundsLabel)
								.addComponent(instructions, GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)
								.addComponent(eventIDLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								//.addComponent(lblErrorMessages)
								)
							.addGap(40))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(43)
							.addComponent(eventIDLabel))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(19)
							.addComponent(questionTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(choicePanel, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
					.addGap(12)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
							.addComponent(instructions, GroupLayout.PREFERRED_SIZE, 157, GroupLayout.PREFERRED_SIZE)
							.addGap(54)
							.addComponent(numberOfRoundsLabel)
							.addGap(79)
							//.addComponent(lblErrorMessages)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(startButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addComponent(linesCanvas, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 413, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(28, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
	}
	
	public ChoicePanel getChoicePanel() {
		return choicePanel;
	}
	
	public DecisionLinesCanvas getLinesCanvas() {
		return linesCanvas;
	}
	public void lockStartButton()
	{
		startButton.setEnabled(false);
	}
	public void setEventID(String id) {
		eventIDLabel.setText("Event ID: " + id);
	}
	public JLabel getQuestionTextField()
	{
		return questionTextField;
	}

	public void setEventIDInstruction() {
		eventIDLabel.setText("Please fill in all the choices.");
		//eventIDLabel.setFont(new Font("Felix Titling", Font.PLAIN, 11));
	}
}
